package com.crdeng.business.service.impl;

import ch.qos.logback.classic.pattern.PropertyConverter;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.crdeng.business.converter.ProductCategoryConverter;
import com.crdeng.business.entity.Product;
import com.crdeng.business.entity.ProductCategory;
import com.crdeng.business.mapper.ProductCategoryMapper;
import com.crdeng.business.service.ProductCategoryService;
import com.crdeng.business.service.ProductService;
import com.crdeng.business.utils.CategoryTreeBuilder;
import com.crdeng.business.utils.ListPageUtils;
import com.crdeng.business.vo.PageVO;
import com.crdeng.business.vo.ProductCategoryVO;
import com.crdeng.handler.BusinessException;
import com.crdeng.response.ResultCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author crdeng
 * @since 2020-12-19
 */
@Service
public class ProductCategoryServiceImpl extends ServiceImpl<ProductCategoryMapper, ProductCategory> implements ProductCategoryService {

    @Autowired
    private ProductService productService;

    /**
     * 分类树
     *
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public PageVO<ProductCategoryVO> getProductCategoryTree(Integer pageNum, Integer pageSize) {
        List<ProductCategory> list = list();
        List<ProductCategoryVO> productCategoryVOS = ProductCategoryConverter.converterToTreeNodeeVo(list);
        List<ProductCategoryVO> productCategoryTree = new CategoryTreeBuilder().categoryTreeBuilder(productCategoryVOS);
        List<ProductCategoryVO> page;
        if (pageSize != null && pageNum != null) {
            page = ListPageUtils.page(productCategoryTree, pageSize, pageNum);
            return new PageVO<ProductCategoryVO>(productCategoryTree.size(), page);
        } else {
            return new PageVO<ProductCategoryVO>(productCategoryTree.size(), productCategoryTree);
        }
    }

    /**
     * 编辑
     *
     * @param productCategoryVO
     */
    @Override
    public void edit(ProductCategoryVO productCategoryVO) {
        ProductCategory productCategory = getById(productCategoryVO.getId());
        //判断用户是否存在
        if (productCategory == null) {
            throw new BusinessException(ResultCode.CATEGORY_NOT_EXIST.getCode(), ResultCode.CATEGORY_NOT_EXIST.getMessage());
        }

        ProductCategory productCategory1 = ProductCategoryConverter.converterToProductCategory(productCategoryVO);
        productCategory1.setModifiedTime(new Date());
        //更新
        updateById(productCategory1);

    }

    /**
     * 删除分类
     *
     * @param id
     */
    @Override
    public void delete(Long id) {
        ProductCategory byId = getById(id);
        //判断分类是否存在
        if (byId == null) {
            throw new BusinessException(ResultCode.CATEGORY_NOT_EXIST.getCode(), ResultCode.CATEGORY_NOT_EXIST.getMessage());
        }
        //判断分类是否有关联
        QueryWrapper<ProductCategory> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("pid", id);
        int count = count(queryWrapper);
        if (count != 0) {
            throw new BusinessException(ResultCode.CATEGORY_EXIST_CHILD_NODE.getCode(),ResultCode.CATEGORY_EXIST_CHILD_NODE.getMessage());
        }
        //判断是否有商品引用了此分类
        QueryWrapper<Product> productQueryWrapper = new QueryWrapper<>();
        productQueryWrapper.eq("one_category_id",id).or()
                .eq("two_category_id",id).or()
                .eq("three_category_id",id);
        int countProduct = productService.count(productQueryWrapper);
        if (countProduct!=0){
            //存在商品联系，无法直接删除
            throw new BusinessException(ResultCode.CATEGORY_EXIST_RELATION.getCode(),ResultCode.CATEGORY_EXIST_RELATION.getMessage());
        }
        //执行删除
       baseMapper.deleteById(id);
    }

    /**
     * 查询分类到二级树
     */
    @Override
    public List<ProductCategoryVO> getProductCategory2Tree() {
        List<ProductCategory> list = list();
        List<ProductCategoryVO> productCategoryVOS = ProductCategoryConverter.converterToTreeNodeeVo(list);
        List<ProductCategoryVO> category2TreeBuilder = new CategoryTreeBuilder().category2TreeBuilder(productCategoryVOS);

        return category2TreeBuilder;
    }

    /**
     * 添加分层
     */
    @Override
    public void add(ProductCategoryVO productCategoryVO) {
      //如果是0则是父类 如果不是则判断是父存是否存在
        if (productCategoryVO.getPid()!=0){
            ProductCategory byId = getById(productCategoryVO.getPid());
            if (byId==null){
                throw new BusinessException(ResultCode.CATEGORY_NOT_EXIST.getCode(),ResultCode.CATEGORY_NOT_EXIST.getMessage());
            }
        }
        QueryWrapper<ProductCategory> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("name",productCategoryVO.getName());
        ProductCategory one = getOne(queryWrapper);
        //判断分层是否存在
        if (one!=null){
            throw  new BusinessException(ResultCode.CATEGORY_ALREADY_EXIST.getCode(),ResultCode.CATEGORY_ALREADY_EXIST.getMessage());
        }


        ProductCategory productCategory = ProductCategoryConverter.converterToProductCategory(productCategoryVO);
        Date date = new Date();
        productCategory.setCreateTime(date);
        productCategory.setModifiedTime(date);
        //添加
        save(productCategory);
    }

    @Override
    public List<Long> getCategoryId() {
        return baseMapper.getCategoryId();
    }
}
